<!DOCTYPE html>
<html><head>
<title>Java - The Learning Experience 4</title>




<meta charset="utf-8">
<meta name="X-UA-Compatible" content="IE=edge">
<meta name="google-site-verification" content="">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="telephone=no" name="format-detection">
<meta name="description" content="">
<meta name="renderer" content="webkit">
<meta name="theme-color" content="#ffffff">



<meta property="og:title" content="Java - The Learning Experience 4" />
<meta property="og:description" content="Exceptions 异常处理  三种类型的异常： 检查性异常：最具代表的检查性异常是用户错误或问题引起的异常，这是程序员无法预见的。例如要打开一个不存在文件时，一个异常就发生了，这些异常在编译时不能被简单地忽略。例如：SQLException, IOException, FileNotFoundException
运行时异常： 运行时异常是可能被程序员避免的异常。与检查性异常相反，运行时异常可以在编译时被忽略。例如：ArithmeticException, ClassCastException, ArrayIndexOutOfBoundsException, NullPointerException
错误： 错误不是异常，而是脱离程序员控制的问题。错误在代码中通常被忽略。例如，当栈溢出时，一个错误就发生了，它们在编译也检查不到的。例如：System malfunction, OutOfMemoryException, StackOverflowError
理解： 1、检查性异常: 不处理编译不能通过 2、非检查性异常:不处理编译可以通过，如果有抛出直接抛到控制台 3、运行时异常: 就是非检查性异常 4、非运行时异常: 就是检查性异常
 Exception 类的层次 所有的异常类是从 java.lang.Exception 类继承的子类。 Exception 类是 Throwable 类的子类。除了Exception类外，Throwable还有一个子类Error。 Java 程序通常不捕获错误。错误一般发生在严重故障时，它们在Java程序处理的范畴之外。 Error 用来指示运行时环境发生的错误。
 Java 内置异常类和方法 链接：https://www.runoob.com/java/java-exceptions.html  关键字 try catch throw finally throws / throw 如果一个方法没有捕获到一个检查性异常，那么该方法必须使用 throws 关键字来声明。throws 关键字放在方法签名的尾部。
也可以使用 throw 关键字抛出一个异常，无论它是新实例化的还是刚捕获到的,多个异常之间用逗号隔开。 下面方法的声明抛出一个 RemoteException 异常：
import java.io.*;public class className{public void deposit(double amount) throws RemoteException{// Method implementationthrow new RemoteException();}//Remainder of class definition} finally 关键字用来创建在 try 代码块后面执行的代码块。 无论是否发生异常，finally 代码块中的代码总会被执行。 在 finally 代码块中，可以运行清理类型等收尾善后性质的语句。 finally 代码块出现在 catch 代码块最后。" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://wondrouswisdomcard.gitee.io/posts/java4/" />
<meta property="article:published_time" content="2020-11-12T00:00:00+00:00" />
<meta property="article:modified_time" content="2020-11-12T00:00:00+00:00" /><meta property="og:site_name" content="My Blog" />





<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Java - The Learning Experience 4"/>
<meta name="twitter:description" content="Exceptions 异常处理  三种类型的异常： 检查性异常：最具代表的检查性异常是用户错误或问题引起的异常，这是程序员无法预见的。例如要打开一个不存在文件时，一个异常就发生了，这些异常在编译时不能被简单地忽略。例如：SQLException, IOException, FileNotFoundException
运行时异常： 运行时异常是可能被程序员避免的异常。与检查性异常相反，运行时异常可以在编译时被忽略。例如：ArithmeticException, ClassCastException, ArrayIndexOutOfBoundsException, NullPointerException
错误： 错误不是异常，而是脱离程序员控制的问题。错误在代码中通常被忽略。例如，当栈溢出时，一个错误就发生了，它们在编译也检查不到的。例如：System malfunction, OutOfMemoryException, StackOverflowError
理解： 1、检查性异常: 不处理编译不能通过 2、非检查性异常:不处理编译可以通过，如果有抛出直接抛到控制台 3、运行时异常: 就是非检查性异常 4、非运行时异常: 就是检查性异常
 Exception 类的层次 所有的异常类是从 java.lang.Exception 类继承的子类。 Exception 类是 Throwable 类的子类。除了Exception类外，Throwable还有一个子类Error。 Java 程序通常不捕获错误。错误一般发生在严重故障时，它们在Java程序处理的范畴之外。 Error 用来指示运行时环境发生的错误。
 Java 内置异常类和方法 链接：https://www.runoob.com/java/java-exceptions.html  关键字 try catch throw finally throws / throw 如果一个方法没有捕获到一个检查性异常，那么该方法必须使用 throws 关键字来声明。throws 关键字放在方法签名的尾部。
也可以使用 throw 关键字抛出一个异常，无论它是新实例化的还是刚捕获到的,多个异常之间用逗号隔开。 下面方法的声明抛出一个 RemoteException 异常：
import java.io.*;public class className{public void deposit(double amount) throws RemoteException{// Method implementationthrow new RemoteException();}//Remainder of class definition} finally 关键字用来创建在 try 代码块后面执行的代码块。 无论是否发生异常，finally 代码块中的代码总会被执行。 在 finally 代码块中，可以运行清理类型等收尾善后性质的语句。 finally 代码块出现在 catch 代码块最后。"/>







<script src="/vendor/js/jquery.min.js" ></script>
<script src="/vendor/js/popper.min.js" ></script>
<script src="/vendor/js/bootstrap.min.js" ></script>
<script src="/vendor/js/smooth-scroll.polyfills.min.js" ></script>
<link type="text/css" rel="stylesheet" href="/vendor/css/bootstrap.min.css">
<script src="/vendor/js/vue.min.js" ></script>






<link rel="stylesheet" href="https://wondrouswisdomcard.gitee.io/scss/journal.min.c116bc90d171283f099f173854157ec8f183f9073b93443b2c8ad82899ee9025.css" integrity="sha256-wRa8kNFxKD8Jnxc4VBV&#43;yPGD&#43;Qc7k0Q7LIrYKJnukCU=" media="screen">



<link rel="stylesheet" href="https://wondrouswisdomcard.gitee.io/scss/dark-mode.min.552aae4638a84aa57cf0b195750a49ea9131d3bb621d1ed3ebc9b14b18166536.css" integrity="sha256-VSquRjioSqV88LGVdQpJ6pEx07tiHR7T68mxSxgWZTY=" media="screen">


<script src="https://wondrouswisdomcard.gitee.io/js/loadCSS.js"></script>
<script>
  loadCSS("https://fonts.googleapis.com/css?family=Lora|Montserrat|Fira+Mono|Noto+Serif+SC|Material+Icons");
</script>




  
    <script src="https://wondrouswisdomcard.gitee.io/js/toc-collapse.js"></script>
  



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script src="/vendor/js/md5.min.js"></script>
<script>
  var gitalk = new Gitalk({
  clientID: 'your client id',
  clientSecret: 'your client secret',
  repo: 'repo name',
  owner: 'user',
  admin: ['user'],
  id: md5(location.pathname),
  distractionFreeMode: 'false'
  });
  window.onload = function () {
        gitalk.render('gitalk-container')
  }
</script>







<script src="https://cdn.jsdelivr.net/npm/vue-disqus@3/dist/vue-disqus.js"></script>


</head>
<body>
    	<div id="app"><div ref="sideContainer" class="side-container">
    
    <a class="a-block nav-head false" href="https://wondrouswisdomcard.gitee.io">
    
        <div class="nav-title">
            Wondrous WindomCard&#39;s Blog
        </div>
        
        <div class="nav-subtitle">
            Themed by Diary.
        </div>
        
    </a>

    <div class="nav-link-list">
        
        
            
            
            
                
            
            
            
            <a class="a-block nav-link-item active" href="/posts">
                Archive
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/categories">
                Categories
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/tags">
                Tags
            </a>
            
        
            
            
            
            
            
            <a class="a-block nav-link-item false" href="/index.xml">
                RSS Feed
            </a>
            
        
    </div>

    

    <div class="nav-footer">
        
Hugo Theme <a href="https://github.com/amazingrise/hugo-theme-diary">Diary</a> by <a href="https://amazingrise.net">Rise</a>
<br>
Ported from <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a>'s <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> <br>
<br>

&copy;
	
	This is a customized copyright.
	

    </div>
    
</div><div ref="extraContainer" class="extra-container">
    
    
    <div class="toc animated-visibility" :class="{ invisible: scrollY <= 140 }">


	<div class="toc-content">
	
		
		
		
		<center>- CATALOG -</center>
		
		
		<ul>
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exceptions-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86" onclick="onNavClick(`#exceptions-异常处理-nav`)" id="exceptions-异常处理-nav">
									Exceptions 异常处理
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#%e4%b8%89%e7%a7%8d%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%bc%82%e5%b8%b8" onclick="onNavClick(`#三种类型的异常-nav`)" id="三种类型的异常-nav">
									三种类型的异常：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exception-%e7%b1%bb%e7%9a%84%e5%b1%82%e6%ac%a1" onclick="onNavClick(`#exception-类的层次-nav`)" id="exception-类的层次-nav">
									Exception 类的层次
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#java-%e5%86%85%e7%bd%ae%e5%bc%82%e5%b8%b8%e7%b1%bb%e5%92%8c%e6%96%b9%e6%b3%95" onclick="onNavClick(`#java-内置异常类和方法-nav`)" id="java-内置异常类和方法-nav">
									Java 内置异常类和方法
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#%e5%85%b3%e9%94%ae%e5%ad%97-try-catch-throw-finally" onclick="onNavClick(`#关键字-try-catch-throw-finally-nav`)" id="关键字-try-catch-throw-finally-nav">
									关键字 try catch throw finally
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#throws--throw" onclick="onNavClick(`#throws--throw-nav`)" id="throws--throw-nav">
									throws / throw
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#finally" onclick="onNavClick(`#finally-nav`)" id="finally-nav">
									finally
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%e7%b1%bb" onclick="onNavClick(`#自定义异常类-nav`)" id="自定义异常类-nav">
									自定义异常类
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#%e5%87%a0%e5%88%99%e7%bb%83%e4%b9%a0" onclick="onNavClick(`#几则练习-nav`)" id="几则练习-nav">
									几则练习
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#exercise-1" onclick="onNavClick(`#exercise-1-nav`)" id="exercise-1-nav">
									Exercise 1
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-2" onclick="onNavClick(`#exercise-2-nav`)" id="exercise-2-nav">
									Exercise 2
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-3" onclick="onNavClick(`#exercise-3-nav`)" id="exercise-3-nav">
									Exercise 3
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-4" onclick="onNavClick(`#exercise-4-nav`)" id="exercise-4-nav">
									Exercise 4
								</a>
							</li>
						
						
					
				
			
		</ul>
	</div>

</div>
    
    <div class="pagination">
        <a id="globalBackToTop" class="pagination-action animated-visibility" href="#top" :class="{ invisible: scrollY == 0 }">
            <i class="material-icons pagination-action-icon">
                keyboard_arrow_up
            </i>
        </a>
        
        <a class="pagination-action" v-on:click="toggleDarkMode">
            <i class="material-icons pagination-action-icon" v-if="isDarkMode">
                brightness_4
            </i>
            <i class="material-icons pagination-action-icon" v-else="isDarkMode">
                brightness_7
            </i>
        </a>
        
        
    </div>
</div>
<div class="single-column-drawer-container" ref="drawer"
     v-bind:class="{ 'single-column-drawer-container-active': isDrawerOpen }">
    <div class="drawer-content">
        <div class="drawer-menu">
            
            
            
                
                
                
                    
                
                
                
                <a class="a-block drawer-menu-item active" href="/posts">
                    Archive
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/categories">
                    Categories
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/tags">
                    Tags
                </a>
                
            
                
                
                
                
                
                <a class="a-block drawer-menu-item false" href="/index.xml">
                    RSS Feed
                </a>
                
            
            
            <div class="toc">


	<div class="toc-content">
	
		
		
		
		<center>- CATALOG -</center>
		
		
		<ul>
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exceptions-%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86" onclick="onNavClick(`#exceptions-异常处理-nav`)" id="exceptions-异常处理-nav">
									Exceptions 异常处理
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#%e4%b8%89%e7%a7%8d%e7%b1%bb%e5%9e%8b%e7%9a%84%e5%bc%82%e5%b8%b8" onclick="onNavClick(`#三种类型的异常-nav`)" id="三种类型的异常-nav">
									三种类型的异常：
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exception-%e7%b1%bb%e7%9a%84%e5%b1%82%e6%ac%a1" onclick="onNavClick(`#exception-类的层次-nav`)" id="exception-类的层次-nav">
									Exception 类的层次
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#java-%e5%86%85%e7%bd%ae%e5%bc%82%e5%b8%b8%e7%b1%bb%e5%92%8c%e6%96%b9%e6%b3%95" onclick="onNavClick(`#java-内置异常类和方法-nav`)" id="java-内置异常类和方法-nav">
									Java 内置异常类和方法
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#%e5%85%b3%e9%94%ae%e5%ad%97-try-catch-throw-finally" onclick="onNavClick(`#关键字-try-catch-throw-finally-nav`)" id="关键字-try-catch-throw-finally-nav">
									关键字 try catch throw finally
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#throws--throw" onclick="onNavClick(`#throws--throw-nav`)" id="throws--throw-nav">
									throws / throw
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#finally" onclick="onNavClick(`#finally-nav`)" id="finally-nav">
									finally
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
							
								</ul>
							
						
						
						
							<li>
								<a href="#%e8%87%aa%e5%ae%9a%e4%b9%89%e5%bc%82%e5%b8%b8%e7%b1%bb" onclick="onNavClick(`#自定义异常类-nav`)" id="自定义异常类-nav">
									自定义异常类
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#%e5%87%a0%e5%88%99%e7%bb%83%e4%b9%a0" onclick="onNavClick(`#几则练习-nav`)" id="几则练习-nav">
									几则练习
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
							
								<ul class="collapse" data-toggle="collapse">
							
						
						
							<li>
								<a href="#exercise-1" onclick="onNavClick(`#exercise-1-nav`)" id="exercise-1-nav">
									Exercise 1
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-2" onclick="onNavClick(`#exercise-2-nav`)" id="exercise-2-nav">
									Exercise 2
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-3" onclick="onNavClick(`#exercise-3-nav`)" id="exercise-3-nav">
									Exercise 3
								</a>
							</li>
						
						
					
				
			
				
				
					
						
						
						
						
						
							<li>
								<a href="#exercise-4" onclick="onNavClick(`#exercise-4-nav`)" id="exercise-4-nav">
									Exercise 4
								</a>
							</li>
						
						
					
				
			
		</ul>
	</div>

</div>
            
        </div>
    </div>
</div>
<transition name="fade">
    <div v-bind:class="{ 'single-column-drawer-mask': mounted }" v-if="isDrawerOpen" v-on:click="toggleDrawer"></div>
</transition>
<nav ref="navBar" class="navbar sticky-top navbar-light single-column-nav-container">
    <div ref="navBackground" class="nav-background"></div>
    <div class="container container-narrow nav-content">
        <button id="nav_dropdown_btn" class="nav-dropdown-toggle" type="button" v-on:click="toggleDrawer">
            <i class="material-icons">
                menu
            </i>
        </button>
        <a ref="navTitle" class="navbar-brand" href="https://wondrouswisdomcard.gitee.io">
            Wondrous WindomCard&#39;s Blog
        </a>
        
        <button type="button" class="nav-darkmode-toggle" v-on:click="toggleDarkMode">
            <i class="material-icons" v-if="isDarkMode">
                brightness_4
            </i>
            <i class="material-icons" v-else="isDarkMode">
                brightness_7
            </i>
        </button>
        
    </div>
</nav>
<div class="single-column-header-container" ref="pageHead"
     v-bind:style="{ transform: 'translateZ(0px) translateY('+.3*scrollY+'px)', opacity: 1-navOpacity }">
    <a href="https://wondrouswisdomcard.gitee.io">
        <div class="single-column-header-title">Wondrous WindomCard&#39;s Blog</div>
        
        <div class="single-column-header-subtitle">Themed by Diary.</div>
        

    </a>
</div>

            <div id="content">
<div ref="streamContainer" class="stream-container">
    <div class="post-list-container post-list-container-shadow">
        <div class="post">
            
            
            

            <div class="post-head-wrapper-text-only"
                
            >
                <div class="post-title">
                    Java - The Learning Experience 4
                    
                    <div class="post-meta">
                        
                        <time itemprop="datePublished">
                            2020-11-12 00:00
                        </time>
                        

                        
                            <i class="material-icons" style="">folder</i>
                                <a href="/categories/">[JavaNote]</a>
                                &nbsp;
                        

                        
                            <i class="material-icons" style="">label</i>
                            
                                <a href="/tags/java">Java</a>
                                &nbsp;
                            
                                <a href="/tags/exceptions">Exceptions</a>
                                &nbsp;
                            
                        
                        
                    </div>
                </div>
            </div>
            
            <div class="post-body-wrapper">
                
                <div class="post-body" v-pre>
                
                    <h1 id="exceptions-异常处理">Exceptions 异常处理</h1>
<hr>
<h3 id="三种类型的异常">三种类型的异常：</h3>
<p><strong>检查性异常</strong>：最具代表的检查性异常是用户错误或问题引起的异常，这是程序员无法预见的。例如要打开一个不存在文件时，一个异常就发生了，这些异常在编译时不能被简单地忽略。例如：SQLException, IOException, FileNotFoundException</p>
<p><strong>运行时异常</strong>： 运行时异常是可能被程序员避免的异常。与检查性异常相反，运行时异常可以在编译时被忽略。例如：ArithmeticException, ClassCastException, ArrayIndexOutOfBoundsException, NullPointerException</p>
<p><strong>错误</strong>： 错误不是异常，而是脱离程序员控制的问题。错误在代码中通常被忽略。例如，当栈溢出时，一个错误就发生了，它们在编译也检查不到的。例如：System malfunction, OutOfMemoryException, StackOverflowError</p>
<p>理解：
1、检查性异常: 不处理编译不能通过
2、非检查性异常:不处理编译可以通过，如果有抛出直接抛到控制台
3、运行时异常: 就是非检查性异常
4、非运行时异常: 就是检查性异常</p>
<hr>
<h3 id="exception-类的层次">Exception 类的层次</h3>
<p>所有的异常类是从 java.lang.Exception 类继承的子类。 Exception 类是 Throwable 类的子类。除了Exception类外，Throwable还有一个子类Error。 Java 程序通常不捕获错误。错误一般发生在严重故障时，它们在Java程序处理的范畴之外。 Error 用来指示运行时环境发生的错误。</p>
<p><img src="../../images/java4/1.png" alt="1"></p>
<hr>
<h3 id="java-内置异常类和方法">Java 内置异常类和方法</h3>
<p>链接：https://www.runoob.com/java/java-exceptions.html
<img src="../../images/java4/2.png" alt="2"></p>
<hr>
<h3 id="关键字-try-catch-throw-finally">关键字 try catch throw finally</h3>
<p><img src="../../images/java4/3.png" alt="3"></p>
<h4 id="throws--throw">throws / throw</h4>
<p>如果一个方法没有捕获到一个检查性异常，那么该方法必须使用 throws 关键字来声明。throws 关键字放在方法签名的尾部。</p>
<p>也可以使用 throw 关键字抛出一个异常，无论它是新实例化的还是刚捕获到的,多个异常之间用逗号隔开。
下面方法的声明抛出一个 RemoteException 异常：</p>
<pre><code>import java.io.*;
public class className
{
public void deposit(double amount) throws RemoteException
{
    // Method implementation
    throw new RemoteException();
}
//Remainder of class definition
}
</code></pre>
<h4 id="finally">finally</h4>
<p>关键字用来创建在 try 代码块后面执行的代码块。
无论是否发生异常，finally 代码块中的代码总会被执行。
在 finally 代码块中，可以运行清理类型等收尾善后性质的语句。
finally 代码块出现在 catch 代码块最后。</p>
<hr>
<h3 id="自定义异常类">自定义异常类</h3>
<p>所有异常都必须是 Throwable 的子类。
如果希望写一个检查性异常类，则需要继承 Exception 类。</p>
<p>下图是几种声明类型：
<img src="../../images/java4/4.png" alt="4"></p>
<hr>
<h3 id="几则练习">几则练习</h3>
<h4 id="exercise-1">Exercise 1</h4>
<p><img src="../../images/java4/6.png" alt="6"></p>
<h4 id="exercise-2">Exercise 2</h4>
<pre><code>class Divide {
    public static void main(String args[]) {
        try {
        System.out.println(&quot;Before Division&quot;);
        int i = Integer.parseInt(args[0]);
        int j = Integer.parseInt(args[1]);
        System.out.println(i / j);
        System.out.println(&quot;After Division&quot;);
        }
        catch (ArithmeticException e) {
        System.out.println(&quot;ArithmeticException&quot;);
        }
        catch (ArrayIndexOutOfBoundsException e) {
        System.out.println(&quot;ArrayIndex&quot; +
        &quot;OutOfBoundsException&quot;);
        }
        catch (NumberFormatException e) {
        System.out.println(&quot;NumberFormatException&quot;);}
        finally {
            System.out.println(&quot;Finally block&quot;);
        }
    }
}
</code></pre>
<p>String args[]为空的输出：</p>
<pre><code>Before Division
ArrayIndexOutOfBoundsException
Finally block
</code></pre>
<h4 id="exercise-3">Exercise 3</h4>
<p><img src="../../images/java4/7.png" alt="7"></p>
<p>输出：//自己写的 有可能有问题子</p>
<pre><code>Before a 
Before b 
Before c 
Before d 
d: finally 
c: finally 
b: e(ArrayIndexOutOfBoundsException) 
b: finally 
After b 
a: finally 
After a 
main: finally
</code></pre>
<h4 id="exercise-4">Exercise 4</h4>
<p><img src="../../images/java4/8.png" alt="8"></p>
<p>输出：//自己写的 有可能有问题子</p>
<pre><code>Before a
Before b
Before c
Before d
d: e
d: finally
c: e
c: finally
b: e
b: finally
After b
a: finally
After a
main: finally
</code></pre>
<hr>
<p>资料来源，参考：</p>
<p><strong>陈老师的Java课网站：http://inpluslab.com:8000/java2020/</strong></p>
<p><strong>菜鸟教程：https://www.runoob.com/java/java-exceptions.html</strong></p>

                    
                    <HR width="100%" id="EOF">
		    <p style="color:#777;">Last modified on 2020-11-12</p>
                    
                </div>
            </div>
            
            
            <nav class="post-pagination">

                
                <a class="newer-posts" href="https://wondrouswisdomcard.gitee.io/posts/blog6/">
			Next<br>Computing By Using Python
                </a>
                
                
                
                <a class="older-posts" href="https://wondrouswisdomcard.gitee.io/posts/java5/">
			Previous<br>Java - The Learning Experience 5
                </a>
                
            </nav>
            <div class="post-comment-wrapper">
                
<div class="comments">
    <vue-disqus shortname="Your disqus short name."></vue-disqus>
</div>
<noscript><noscript>Please activate JavaScript to view comments.</noscript></noscript>
<a href="https://disqus.com/" class="dsq-brlink">Comments powered by <span class="logo-disqus">Disqus</span></a>



<div id="gitalk-container"></div>






            </div>
        </div>
    </div>
</div>

            </div><div id="single-column-footer">
Hugo Theme <a href="https://github.com/amazingrise/hugo-theme-diary">Diary</a> by <a href="https://amazingrise.net">Rise</a>
<br>
Ported from <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a>'s <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> <br>
<br>

&copy;
	
	This is a customized copyright.
	
</div>
            </div>
    <script>
let app;

app = new Vue({
    el: '#app',
    data: {
        scrollY: 0,
        navOpacity: 0,
        isDrawerOpen: false,
        mounted: false,
        isDarkMode: false
    },
    methods: {
            sgn(t, x) {
                let k = 1. / (1. - 2 * t);
                if (x <= t) return 0;
                else if (x >= 1 - t) return 1;
                else {
                    return k * (x - t);
                }
            },
            handleScroll() {
                this.scrollY = window.scrollY;
                this.navOpacity = this.sgn(.0, Math.min(1, Math.max(0, window.scrollY / (this.pageHeadHeight() - this.navBarHeight() * 0.8))));
                const {navBar, navBackground, navTitle, extraContainer, streamContainer} = this.$refs;

                if (this.navOpacity >= 1) {
                    navBackground.style.opacity = 1;
                    navTitle.style.opacity = 1;
                } else {
                    navBackground.style.opacity = 0;
                    navTitle.style.opacity = 0;
                }
            },
            handleResize() {
                const {navBar, navBackground, navTitle, extraContainer, streamContainer} = this.$refs;
                extraContainer.style.left = (streamContainer.offsetWidth - extraContainer.offsetWidth) + 'px';
            },
            navBarHeight() {
                return this.$refs.navBar.offsetHeight;
            },
            pageHeadHeight() {
                return this.$refs.pageHead.offsetHeight;
            },
            toggleDrawer() {
                this.isDrawerOpen = !this.isDrawerOpen;
                document.getElementsByTagName('html')[0].style.overflow = this.isDrawerOpen ? 'hidden' : 'unset';
            },
            closeDrawer() {
                this.isDrawerOpen = false;
                document.getElementsByTagName('html')[0].style.overflow = this.isDrawerOpen ? 'hidden' : 'unset';
            },
            toggleDarkMode() {
                this.isDarkMode = !this.isDarkMode;
                if (this.isDarkMode==true){
                    document.cookie = "night=1;path=/";
                    document.body.classList.add("night");
                } else {
                    document.cookie = "night=0;path=/";
                    document.body.classList.remove("night");
                }
            }
    },
    created() {
        window.addEventListener('scroll', this.handleScroll);
        window.addEventListener('resize', this.handleResize);
        window._nonDesktop = function () {
            let check = false;
            (function (a) {
                if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true;
            })(navigator.userAgent || navigator.vendor || window.opera);
            return check;
        };
        
        var night = document.cookie.replace(/(?:(?:^|.*;\s*)night\s*\=\s*([^;]*).*$)|^.*$/, "$1");
        if (night==""){
            if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                
            }
        }else{
            
            if (night=="1") {
                this.toggleDarkMode();
            }
        }
    },
    mounted() {
        this.handleScroll();
        this.handleResize();
        this.mounted = true;

        
    },
    destroyed() {
        window.removeEventListener('scroll', this.handleScroll);
        window.removeEventListener('resize', this.handleResize);
    }
});
</script>

<script src="https://wondrouswisdomcard.gitee.io/js/journal.js"></script>
    </body>
</html>
